#include "CPlan.h"
#include "CTimeLine.h"

CPlan::CPlan()
{
    m_pFirstTimeLine = nullptr;
	m_pNextPlan      = nullptr;
	m_pPrePlan       = nullptr;
}

CPlan::~CPlan()
{
	if(m_pFirstTimeLine) delete m_pFirstTimeLine;
}

//Main Method
void CPlan::setName(std::string name) {
	m_strName = name;
}

void CPlan::setLabel(std::string label){
    m_strLabel = label;
}

void CPlan::addLabel(std::string label) {
	m_strLabel += label + " ";
}

void CPlan::setRemarks(std::string remarks) {
    m_strRemarks = remarks;
}

std::string CPlan::getName() const {
	return m_strName;
}

std::string CPlan::getLabel() const {
	return m_strLabel;
}

std::string CPlan::getRemarks() const {
	return m_strRemarks;
}

int CPlan::add(IPlan* plan)
{
	int result = 0;

	if (this->m_pNextPlan) {
		result = m_pNextPlan->add(plan);
	}
	else {
		m_pNextPlan = plan;
		return 0;
	}

	return result;
}

int CPlan::remove(IPlan* plan)
{
	int result = 0;

	if (m_strName == plan->getName()) {
		// Set next
		if (m_pNextPlan) {
			m_pNextPlan->setPre(m_pPrePlan);
		}

		// Set pre
		if (m_pPrePlan) {
			m_pPrePlan->setNext(m_pNextPlan);
		}
	}
	else {
		if (m_pNextPlan) {
			result = m_pNextPlan->remove(plan);
		}
		else {
			return 1; // Cannot find such a plan
		}
	}

	return 0;
}

IPlan* CPlan::find(std::string name)
{
	if (m_strName == name) {
		return this;
	}
	else if(m_pNextPlan){
		return m_pNextPlan->find(name);
	}
	else {
		return nullptr;
	}
}

IPlan* CPlan::find(int index)
{
    if(index == 0)
        return this;
    
    IPlan *iPtr = this;
    int count = 0;
    while (iPtr->getNext()) {
        if(count == index)
            return iPtr;
        
        count++;
        iPtr = const_cast<IPlan*>(iPtr->getNext());
    }
    
    return nullptr;
}

int CPlan::addTimeLine(ITimeLine* newTimeLine) {
	if (m_pFirstTimeLine) {
		if (!m_pFirstTimeLine->add(newTimeLine))
			return 0;
		else
			return 1;
	}
	else {
		m_pFirstTimeLine = newTimeLine;
        
		return 0;
	}
}

int CPlan::removeTimeLine(std::string name)
{
	if (m_pFirstTimeLine)
	{
		if (!m_pFirstTimeLine->remove(m_pFirstTimeLine->find(name)))
			return 0;
		else
			return 1;
	}
	else {
		return -1; // nullptr error
	}
}

ITimeLine* CPlan::getTimeLine(std::string name) const {
    if(m_pFirstTimeLine->getName() == name)
        return m_pFirstTimeLine;
    
    ITimeLine* ptr = m_pFirstTimeLine;
    
    while (ptr->getNext()) {
        if(ptr->getName() == name)
            return ptr;
        
        ptr = ptr->getNext();
    }
    
    return nullptr;
}

ITimeLine* CPlan::getTimeLine(int index) const{
    if(index == 0)
        return m_pFirstTimeLine;
    
    ITimeLine* ptr = m_pFirstTimeLine;
    int count = 0;
    
    while(ptr->getNext()){
        if(count == index)
            return ptr;
        
        ptr = ptr->getNext();
        count++;
    }
    
    return nullptr;
}

bool CPlan::operator==(IPlan& p)
{
	return p.getName() == m_strName;
}

void CPlan::setNext(IPlan* plan)
{
	m_pNextPlan = plan;
}

void CPlan::setPre(IPlan* plan)
{
	m_pPrePlan = plan;
}

const IPlan* CPlan::getNext() const
{
	return m_pNextPlan;
}

const IPlan* CPlan::getPre() const
{
	return m_pPrePlan;
}
